package com.zjh.photoshopstudy.app.register;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zjh.photoshopstudy.app.MainActivity;
import com.zjh.photoshopstudy.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegisterActivity extends AppCompatActivity {
    private RegisterPresent mPresent;

    private EditText mUsernameEdit;
    private EditText mPasswordEdit;
    private EditText mConfirmPasswordEdit;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        mUsernameEdit = findViewById(R.id.username_edit);
        mPasswordEdit = findViewById(R.id.password_edit);
        mConfirmPasswordEdit = findViewById(R.id.confirm_psw_edit);
        btn = findViewById(R.id.register_btn);
        mPresent = new RegisterPresent(this);
        mUsernameEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (!b) {
                    String username = mUsernameEdit.getText().toString().trim();
                    if (isEmail(username)) {
                        mPresent.checkUserNmae(username);
                    } else {
                        Toast.makeText(RegisterActivity.this, "请输入正确邮箱！", Toast.LENGTH_SHORT).show();
                        mUsernameEdit.setBackgroundResource(R.drawable.dialog_bg2);
                        btn.setEnabled(false);
                    }
                }
            }
        });
    }

    public static boolean isEmail(String email) {
        if (null == email || "".equals(email)) return false;
        //Pattern p = Pattern.compile("\\w+@(\\w+.)+[a-z]{2,3}"); //简单匹配
        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
        Matcher m = p.matcher(email);
        return m.matches();
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.register_btn:
                doRegister();
                break;
        }

    }

    private void doRegister() {
        String username = mUsernameEdit.getText().toString().trim();
        String password = mPasswordEdit.getText().toString().trim();
        String confirmPassword = mConfirmPasswordEdit.getText().toString().trim();
        if (!password.equals(confirmPassword)) {
            Toast.makeText(this, "两次输入密码不相同！", Toast.LENGTH_SHORT).show();
            return;
        }
        mPresent.register(username, password, confirmPassword);
    }

    public void onRegisterSuccess() {
        Toast.makeText(this, "注册成功！", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    public void onRegisterFail() {
        Toast.makeText(this, "注册失败！", Toast.LENGTH_SHORT).show();
    }

    public void onCheckSuccess() {
        mUsernameEdit.setBackgroundResource(R.drawable.dialog_bg);
        btn.setEnabled(true);
    }

    public void onCheckFail() {
        Toast.makeText(this, "用户名已存在！", Toast.LENGTH_SHORT).show();
        mUsernameEdit.setBackgroundResource(R.drawable.dialog_bg2);
        btn.setEnabled(false);
    }
}
